package com.atguigu.ssyx.acl.controller;


import com.atguigu.ssyx.acl.server.AdminService;
import com.atguigu.ssyx.acl.server.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * ClassName: AdminController
 * Package: com.atguigu.ssyx.acl.controller
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 * Date: 2023-09-19
 * Time: 21:52
 */

@Api(tags = "用户接口")
@RestController
@RequestMapping("/admin/acl/user")

public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RoleService roleService;

    //为用户分配角色
    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam Long adminId,
                           @RequestParam Long[] roleId){
        roleService.saveAdminRole(adminId,roleId);
        return Result.ok(null);
    }

    //分配角色
    //查询所有角色 和 根据用户id查询用户分配角色列表
    //返回Map集合 刚好是对应的Key-Val
    @ApiOperation("获取某个用户的所有角色")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable Long adminId){
        Map<String,Object> map = roleService.getRoleByAdminId(adminId);
        return Result.ok(map);
    }


    //Admin接口
    //1.用户查询
    @ApiOperation("用户分页查询")
    @GetMapping("/{page}/{limit}")
    public Result pageUser(@PathVariable Long page,
                           @PathVariable Long limit,
                           AdminQueryVo adminQueryVo) {
        //创建分页对象
        Page<Admin> adminPage = new Page<>(page, limit);
        //通过service查询
        IPage<Admin> iPage = adminService.selectPage(adminPage, adminQueryVo);
        //返回查询结果
        return Result.ok(iPage);
    }

    //2.id查询
    @ApiOperation("根据ID查询用户")
    @GetMapping("/get/{id}")
    public Result getById(@PathVariable Long id) {
        Admin byId = adminService.getById(id);
        return Result.ok(byId);
    }

    //3.添加用户
    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin) {

        //取出密码 然后加密
        admin.setPassword(MD5.encrypt(admin.getPassword()));
        boolean save = adminService.save(admin);
        return Result.ok(null);

    }

    //4.修改用户
    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
        boolean b = adminService.updateById(admin);
        return Result.ok(null);
    }

    //5.id删除
    @ApiOperation("根据ID删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean b = adminService.removeById(id);
        return Result.ok(null);
    }

    //6.批量删除
    @ApiOperation("根据ID批量删除")
    @DeleteMapping("/batchRemove")
    public Result remove(@RequestBody List<Long> ids) {
        boolean b = adminService.removeByIds(ids);

        return Result.ok(null);
    }


}
